package xx.services.entity.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.bas.Reply;
import xx.services.Passport;
import xx.services.entity.inter.IReplyInterface;
import xx.services.util.Constants;
import xx.util.Arr;

/**
 * 评论回复服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/reply")
 * @author Administrator
 * 
 * 2010-12-19
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/reply")
public class ReplyRest extends Passport implements IReplyInterface
{

    public ReplyRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 为实体评论{comment_id}增加一条回复。
     * 
     * @POST
     * @Path ("/{comment_id}")
     * @param commentId
     * @param title
     * @param content
     * @return
     * @author dengjinye,2010-10-29
     */
    @POST
    @Path("/{comment_id}")
    public String addReply(@PathParam("comment_id")
    long commentId, @FormParam("title")
    String title, @FormParam("content")
    String content)
    {
        try
        {
            Reply reply = new Reply(qr, owner);
            reply.setCommentId(commentId).setTitle(title).setContent(content);
            reply.insert();
            return ok(reply);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 删除回复{reply_ids}集
     * 
     * @DELETE
     * @Path ("/{reply_id}")
     * @param replyId
     *            多个ID用“_”连接起来
     * @return
     * @author dengjinye,2010-10-29
     */
    @DELETE
    @Path("/{reply_id}")
    public String deleteReply(@PathParam("reply_id")
    String replyId)
    {
        try
        {
            long[] replyIdArr = Arr.toLongArray(replyId, "_");
            for (long id : replyIdArr)
            {
                Reply reply = new Reply(qr, owner);
                reply.setReplyId(id);
                reply.delete();
            }
            return ok(true);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 描述：1）此接口为受保护接口；2）修改回复{reply_id}的标题内容等。
     * 
     * @PUT
     * @Path ("/{reply_id}")
     * @param replyId
     * @param title
     * @param content
     * @return
     * @author dengjinye,2010-10-29
     */
    @PUT
    @Path("/{reply_id}")
    public String updateReply(@PathParam("reply_id")
    long replyId, @FormParam("title")
    String title, @FormParam("content")
    String content,@FormParam("readed")
    boolean readed)
    {
        try
        {
            Reply reply = new Reply(qr, owner);
            reply.setReplyId(replyId).setTitle(title).setContent(content);
            reply.update(Reply.TITLE, Reply.CONTENT);
            return ok(true);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }
}
